---
--类名: BubbleView.lua
--创建日期: 2015-5-7
--创建人: 陈小虎
--功能描述: 头顶对话泡泡视图
--修改内容：
--修改日期:
--修改人:
--修改备注:
--
local NodeIsShow = cc.Node.isVisible

local BubbleView = class("BubbleView",function()
    return display.newLayer()
end)

function BubbleView:ctor()

    self:init()
    --local InsetsRect = cc.rect(15,40,5,5)--
    -- self.mUINode = cc.uiloader:load(Res.Talk_Bubble)
    -- self:addChild(self.mUINode)

    -- self.mContentBox = self.mUINode:getChildByName("Image_1")--ccui.Scale9Sprite:create(InsetsRect,"public/public_talk.png")
    -- self.mContentBox:setAnchorPoint(cc.p(0,0))

    -- self.mTextNode = self.mContentBox:getChildByName("Text")



    -- local boxSize = self.mContentBox:getContentSize()
    -- local textNodeSize = self.mTextNode:getContentSize()
    -- self.mBoxWidth = textNodeSize.width


    -- self.mTextNode:ignoreContentAdaptWithSize(false)
    -- self.mTextNode:setTextAreaSize(cc.size(self.mBoxWidth,0))

    -- self.mOffsetWidth = (boxSize.width - textNodeSize.width)
    -- self.mOffsetHeight = boxSize.height - textNodeSize.height

    -- self.mTextNode:setString(" ")

    -- -- local fontSize = self.mTextNode:getFontSize()
    -- -- local fontName = self.mTextNode:getFontName()

    -- -- self.mContentLabel = display.newTTFLabel({
    -- --     text = " ",
    -- --     font = fontName,
    -- --     size = fontSize,
    -- --     --align = cc.TEXT_ALIGNMENT_CENTER -- 文字内部居中对齐
    -- --     dimensions = cc.size(self.mBoxWidth, 0)
    -- -- })
    -- -- self.mContentLabel:setAnchorPoint(cc.p(0.5,0.5))
    -- -- self:addChild(self.mContentLabel)
    -- -- self.mContentLabel:setVisible(false)



    -- local pointAction = cc.CSLoader:createTimeline(Res.Talk_Bubble_dian)
    -- self.mUINode:runAction(pointAction)
    -- pointAction:gotoFrameAndPlay(0,pointAction:getDuration(),true)

    -- self.mBoxHeight = self.mTextNode:getContentSize().height + 40--self.mContentLabel:getContentSize().height + 40
    -- self.mOldHeight = self.mBoxHeight
    -- --self.mContentBox:setScale9Enabled(true)
    -- self.mContentBox:setContentSize(cc.size(self.mBoxWidth,self.mBoxHeight))
    -- --self.mContentBox:setTextureRect(cc.rect(0,0,self.mBoxWidth,self.mBoxHeight))
end

function BubbleView:init()
    self._startShowTime = 0

    self:setAnchorPoint(0.5,0)
    self.mUINode = cc.uiloader:load(Res.Talk_Bubble)
    self:addChild(self.mUINode)

    local contentBox = self.mUINode:getChildByName("Image_1")--ccui.Scale9Sprite:create(InsetsRect,"public/public_talk.png")

    self.mContentBox = contentBox
    contentBox:setAnchorPoint(cc.p(0,0))

    local textNode = contentBox:getChildByName("Text")

    self.mTextNode = textNode

    self.mArrowNode = contentBox:findChild("arrow")
    self.mArrowNode:setVisible(true)

    local boxWidth,boxHeight = contentBox:getContentSize2()
    local textNodeWidth, textNodeHeight = textNode:getContentSize2()
    self.mBoxWidth = textNodeWidth
    self.mBoxHeight = textNodeHeight


    textNode:ignoreContentAdaptWithSize(true)
    local textNodeRenderer = textNode:getVirtualRenderer()
    textNodeRenderer:setMaxLineWidth(textNodeWidth)
    textNodeRenderer:setLineBreakWithoutSpace(true)

    self.mOffsetWidth = boxWidth - textNodeWidth
    self.mOffsetHeight = boxHeight - textNodeHeight

    textNode:setString(" ")

    local fontSize = textNode:getFontSize()
    local fontName = textNode:getFontName()

    -- self.mContentLabel = display.newTTFLabel({
    --     text = " ",
    --     font = fontName,
    --     size = fontSize,
    --     --align = cc.TEXT_ALIGNMENT_CENTER -- 文字内部居中对齐
    --     dimensions = cc.size(self.mBoxWidth, 0)
    -- })
    -- self.mContentLabel:setAnchorPoint(cc.p(0.5,0.5))
    -- self:addChild(self.mContentLabel)
    -- self.mContentLabel:setVisible(false)

    -- self.mHideText = ccui.Text:create()
    -- self.mHideText:setFontName(fontName)
    -- self.mHideText:setFontSize(fontSize)
    -- self:addChild(self.mHideText)
    -- self.mHideText:setVisible(false)



    local pointAction = cc.CSLoader:createTimeline(Res.Talk_Bubble_dian)
    self.mUINode:runAction(pointAction)
    pointAction:gotoFrameAndPlay(0,pointAction:getDuration(),true)

    self:setNodeEventEnabled(true)
end


function BubbleView:onEnter()

end

function BubbleView:onExit()
    if self.mUINode then
        self.mUINode:stopAllActions()
    end
end

function BubbleView:show()
    if not self:isVisible() then
        self:addTouchListener()
        self:setVisible(true)
        self._startShowTime = app:getTime()
    end
end

function BubbleView:hide()
    if self:isVisible() then
        self:removeTouchListener()
        self:setVisible(false)
        self.mUINode:stopAllActions()
    end
end

function BubbleView:isShow()
    return NodeIsShow(self)
end

function BubbleView:setContent(content)
    --self.mHideText:setTextAreaSize(cc.size(self.mBoxWidth,0))

    -- self.mHideText:setString(content)
    -- local htWidth = self.mHideText:getVirtualRendererSize().width
    -- if htWidth > self.mBoxWidth then
    --     local rlen = math.floor(htWidth - self.mBoxWidth)
    --     htWidth = math.floor(self.mBoxWidth - rlen)
    -- end



    --self.mTextNode:setTextAreaSize(cc.size(htWidth,0))

    -- self.mTextNode:setString(content)
    self.mTextNode:setVisible(false)
    local height = self.mTextNode:getVirtualRendererSize().height

    -- local redundantLen = 0
    -- local function countRedundantLen()
    --     if width > self.mBoxWidth then
    --         redundantLen = math.floor(width - self.mBoxWidth) + 5
    --     else
    --         redundantLen = 0
    --     end
    -- end
    -- countRedundantLen()

    -- if redundantLen > 0 then
    --     local tWidth = math.floor(self.mBoxWidth - redundantLen)
    --     self.mTextNode:setTextAreaSize(cc.size(tWidth,0))
    --     self.mTextNode:setString(content)
    --     width = self.mTextNode:getVirtualRendererSize().width
    --     height = self.mTextNode:getVirtualRendererSize().height
    -- end

    -- while redundantLen > 0
    -- do
    --     local tWidth = math.floor(self.mBoxWidth - redundantLen)
    --     width = tWidth
    --     self.mTextNode:setTextAreaSize(cc.size(width,0))
    --     width = self.mTextNode:getVirtualRendererSize().width
    --     countRedundantLen()
    -- end

    -- local bWidth = self.mBoxWidth + self.mOffsetWidth
    -- -- local aWidth = width + self.mOffsetWidth
    -- -- if aWidth > bWidth then
    -- --     bWidth = aWidth
    -- -- end

    -- local bHeight = height + self.mOffsetHeight
    local outlineSize
    local outlineColor
    if self.mTextNode.getLabelEffectType and self.mTextNode.getOutlineSize and self.mTextNode.getEffectColor then
        local eType = self.mTextNode:getLabelEffectType()
        if eType == 1 then
            outlineSize = self.mTextNode:getOutlineSize()
            outlineColor = self.mTextNode:getEffectColor()
        end
    end

    local textNodeName = "custom_text_node"
    local fontSize = self.mTextNode:getFontSize()
    local name = UD:getPlayer().name
    local jobStr = L(GD:queryClass(UD:getPlayer().classID).string)
    local Wildcard = require("app.public.wildcard.Wildcard")
    local wildcard = Wildcard.new()
    local textNode,tWidth,tHeight = nil,0,0
    if wildcard:isNewStruct(content) then
        textNode,tWidth,tHeight = wildcard:buildTextNodeByString(content,fontSize,self.mBoxWidth,Res.defaultFont)
    else
        local wordsTab = wildcard:parseWildcardStrings(content,fontSize,jobStr,"sex",name)
        textNode,tWidth,tHeight = wildcard:buildTextNode(wordsTab,self.mBoxWidth,fontName,outlineColor,outlineSize)
    end

    textNode:setName(textNodeName)

    -- 是否添加过Node（主要是为了减少打印错误日志） 
    if self._alreadyAddNode then
        self._alreadyAddNode = false
        self.mContentBox:removeChildByName(textNodeName)
    end
    
    self.mContentBox:addChild(textNode)
    self._alreadyAddNode = true

    textNode:setPosition(cc.p(self.mOffsetWidth / 2,self.mOffsetHeight / 2 + tHeight))

    local bWidth = tWidth + self.mOffsetWidth
    local bHeight = tHeight + self.mOffsetHeight

    self.mContentBox:setContentSize(cc.size(bWidth,bHeight))
    self.mContentBox:setPosition(cc.p(0 - bWidth / 2,self.mContentBox:getPositionY()))
    self.mUINode:setContentSize(cc.size(bWidth,bHeight))
    ccui.Helper:doLayout(self.mUINode)

    if self.mArrowNode then
        self.mArrowNode:setPosition(cc.p(bWidth / 2,self.mArrowNode:getPositionY()))
    end



    -- local scale = self:getScale()
    -- local parent = self:getParent()
    -- while parent
    -- do
    --     scale = parent:getScale() * scale

    --     parent = parent:getParent()
    -- end


    -- self.mContentLabel:setString(content)
    -- self.mTextNode:setString(content)

    -- local width = self.mContentLabel:getContentSize().width
    -- local height = self.mContentLabel:getContentSize().height

    -- local bWidth = width + self.mOffsetWidth
    -- local bHeight = height + self.mOffsetHeight

    -- self.mTextNode:setTextAreaSize(cc.size(width,height))
    -- self.mContentBox:setContentSize(cc.size(bWidth,bHeight))
    -- self.mContentBox:setPosition(cc.p(0 - bWidth / 2,self.mContentBox:getPositionY()))
    -- self.mUINode:setContentSize(cc.size(bWidth,bHeight))
    -- ccui.Helper:doLayout(self.mUINode)
    -- --self.mContentBox:setTextureRect(cc.rect(0,0,self.mBoxWidth,self.mBoxHeight))
end

function BubbleView:faceToLeft()
    -- self.mContentBox:setScaleX(-1)
    -- self.mTextNode:setScaleX(-1)

    -- self.mContentBox:setAnchorPoint(cc.p(0,0))
    -- --self.mContentLabel:setPosition(cc.p(0 - self.mBoxWidth / 2,self.mBoxHeight / 2))

end

function BubbleView:faceToRight()
    -- self.mContentBox:setScaleX(1)
    -- self.mTextNode:setScaleX(1)
    -- self.mContentBox:setAnchorPoint(cc.p(0,0))
    -- --self.mContentLabel:setPosition(cc.p(self.mBoxWidth / 2,self.mBoxHeight / 2))
end

function BubbleView:getWidth()
    local width,height = self.mUINode:getContentSize2()
    return width
end

function BubbleView:getHeight()
    local width,height = self.mContentBox:getContentSize2()
    return self.mContentBox:getPositionY() + height
end

function BubbleView:runAnimation()
    local function call()
        self:show()
    end

    -- local action = cc.Sequence:create(
    --     cc.ScaleTo:create(0,0),
    --     cc.CallFunc:create(call),
    --     cc.ScaleTo:create(0.2,1.2),
    --     cc.ScaleTo:create(0.1,0.9),
    --     cc.ScaleTo:create(0.1,1.0))
    -- self:runAction(action)
    call()
    local action = cc.CSLoader:createTimeline(Res.Talk_Bubble)
    self.mUINode:runAction(action)
    action:gotoFrameAndPlay(0,action:getDuration(),false)
end

function BubbleView:setTouchEndCallback(callback)
    self.mTouchEndCallback = callback
end

function BubbleView:addTouchListener()

    if self.mTouchListener then return end

    self.mTouchListener = cc.EventListenerTouchOneByOne:create()
    self.mTouchListener:setSwallowTouches(false)
    self.mTouchListener:registerScriptHandler(function(touch, event)
        return true
    end, cc.Handler.EVENT_TOUCH_BEGAN)

    self.mTouchListener:registerScriptHandler(function(touch, event)
        if self._startShowTime == 0 then return end
        local cTime = app:getTime()
        local iTime = cTime - self._startShowTime
        if iTime >= 0.5 then
            self._startShowTime = 0
            if self.mTouchEndCallback then
                self.mTouchEndCallback()
            end
        end
    end, cc.Handler.EVENT_TOUCH_ENDED)
    self:getEventDispatcher():addEventListenerWithSceneGraphPriority(self.mTouchListener, self)
end

function BubbleView:removeTouchListener()
    if self.mTouchListener then
        self:getEventDispatcher():removeEventListener(self.mTouchListener)
        self.mTouchListener = nil
    end
end

return BubbleView
